<?php
Yii::import('application.widgets.chart.AbstractExtJsChart');

abstract  class AbstractTeamMonthlyChart extends AbstractExtJsChart{

	public $monthSpan=12;

	public function run(){
		$fields=array_values(CHtml::listData(Team::model()->findAll(array('condition'=>'id != 100')),'id','name'));
		$fieldsJson=CJSON::encode($fields);
		$allFieldsJson=CJSON::encode(CMap::mergeArray(array('month'),$fields ));
		$data=$this->getData();
		$dataJson=CJSON::encode($data);
		$js=<<<EOF
Ext.onReady(function () {
    var store = Ext.create('Ext.data.JsonStore', {
        fields: {$allFieldsJson},
        data: {$dataJson}
    });

    var panel1 = Ext.create('widget.panel', {
        width: {$this->width},
        height: {$this->height},
        title: '{$this->title}',
        renderTo: '{$this->getId()}',
        layout: 'fit',
        items: {
            xtype: 'chart',
            animate: true,
            shadow: true,
            store: store,
            legend: {
                position: 'bottom'
            },
            axes: [{
                type: 'Numeric',
                position: 'left',
                fields: {$fieldsJson},
                title: false,
                grid: true,
                label: {
                    renderer: function(v) {
                        return v;//String(v).replace(/000000$/, 'M');
                    }
                },
                majorTickSteps: 1,
                decimals : 0
            }, {
                type: 'Category',
                position: 'bottom',
                fields: ['month'],
                title: false,
                label: {
		            rotate: {
		                degrees: 315
		            }
		        }
            }],
            series: [{
                type: 'column',
                axis: 'left',
                gutter: 80,
                xField: 'month',
                yField: {$fieldsJson},
                stacked: true,
                tips: {
                    trackMouse: true,
                    width: 100,
                    height: 28,
                    renderer: function(storeItem, item) {
                        this.setTitle(String(item.value[1]));
                    }
                }
            }]
        }
    });
});	
EOF;
		$this->registerChartScript($js);
		parent::run();
	}

	protected function getData(){
		$date=getdate();
		$year=$date['year'];
		$month=$date['mon'];
		$result=array();
		for($i=0;$i<$this->monthSpan;$i++){
			array_unshift($result, $this->getMonthData($year,$month));
			$month=$month-1;
			if($month<1){
				$month=$month+12;
				$year=$year-1;
			}
		}
		return $result;
	}

	protected abstract function getMonthData($year,$month);
}